import Vue from 'vue'

const state = {
  stageList: {},
  stageListDefault: {
    stageId: '',
    stageName: ''
  }
}

const getters = {
  getstageList: state => {
    return state.stageList
  },
  getstageListDefault: state => {
    return state.stageListDefault
  }
}

const mutations = {
  mustageList (state, payload) {
    const obj = {}
    payload.list.forEach((x, i) => {
      if (i === 0) {
        Vue.prototype.$store.commit('mustageListDefault', x)
      }
      obj[x.stageId] = x.stageName
    })
    state.stageList = obj
  },
  mustageListDefault (state, payload) {
    state.stageListDefault.stageId = payload.stageId
    state.stageListDefault.stageName = payload.stageName
  },
  removePersonProjectDefault () {
    state.stageListDefault.stageId = ''
    state.stageListDefault.stageName = ''
  }
}

const actions = {
  actstageList ({ commit }, payload) {
    return new Promise((resolve, reject) => {
      const pjId = Vue.prototype.$store.getters.currentUser.pjId
      Vue.prototype.$axios({
        method: 'get',
        url: `${Vue.prototype.$site.videoServer}/api/constructionList/stage?pjId=${pjId}`,
        closeSaveRequest: true
      }).then(res => {
        if (res.data.errorMsg) {
          resolve(res)
          return
        }
        // 成功回调
        commit('removePersonProjectDefault')
        commit({
          type: 'mustageList',
          list: res.data.data
        })
        resolve(res)
      }, err => {
        reject(err)
      })
    })
  }
}

export default {
  state,
  getters,
  mutations,
  actions
}
